Position Management


In [1]:
import pandas as pd
import oandapyV20
import oandapyV20.endpoints.positions as positions
import configparser

In [2]:
config = configparser.ConfigParser()
config.read('../config/config_v20.ini')
accountID = config['oanda']['account_id']
access_token = config['oanda']['api_key']

In [3]:
client = oandapyV20.API(access_token=access_token)

List all Positions for an Account.


In [4]:
r = positions.PositionList(accountID=accountID)

In [5]:
client.request(r)


Out[5]:
{'lastTransactionID': '77',
 'positions': [{'instrument': 'NZD_USD',
   'long': {'pl': '1.6491',
    'resettablePL': '1.6491',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'pl': '1.6491',
   'resettablePL': '1.6491',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.0000'},
  {'instrument': 'AUD_USD',
   'long': {'averagePrice': '0.75481',
    'pl': '0.0000',
    'resettablePL': '0.0000',
    'tradeIDs': ['31', '33'],
    'units': '200',
    'unrealizedPL': '0.2706'},
   'pl': '0.0000',
   'resettablePL': '0.0000',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.2706'},
  {'instrument': 'GBP_USD',
   'long': {'averagePrice': '1.25892',
    'pl': '0.2866',
    'resettablePL': '0.2866',
    'tradeIDs': ['74'],
    'units': '100',
    'unrealizedPL': '0.0555'},
   'pl': '0.2866',
   'resettablePL': '0.2866',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.0555'},
  {'instrument': 'EUR_USD',
   'long': {'pl': '-0.0086',
    'resettablePL': '-0.0086',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'pl': '-0.0086',
   'resettablePL': '-0.0086',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.0000'}]}

In [6]:
print(r.response)


{'lastTransactionID': '77', 'positions': [{'resettablePL': '1.6491', 'instrument': 'NZD_USD', 'unrealizedPL': '0.0000', 'long': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '1.6491', 'resettablePL': '1.6491'}, 'short': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '0.0000', 'resettablePL': '0.0000'}, 'pl': '1.6491'}, {'resettablePL': '0.0000', 'instrument': 'AUD_USD', 'unrealizedPL': '0.2706', 'long': {'averagePrice': '0.75481', 'units': '200', 'pl': '0.0000', 'unrealizedPL': '0.2706', 'tradeIDs': ['31', '33'], 'resettablePL': '0.0000'}, 'short': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '0.0000', 'resettablePL': '0.0000'}, 'pl': '0.0000'}, {'resettablePL': '0.2866', 'instrument': 'GBP_USD', 'unrealizedPL': '0.0555', 'long': {'averagePrice': '1.25892', 'units': '100', 'pl': '0.2866', 'unrealizedPL': '0.0555', 'tradeIDs': ['74'], 'resettablePL': '0.2866'}, 'short': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '0.0000', 'resettablePL': '0.0000'}, 'pl': '0.2866'}, {'resettablePL': '-0.0086', 'instrument': 'EUR_USD', 'unrealizedPL': '0.0000', 'long': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '-0.0086', 'resettablePL': '-0.0086'}, 'short': {'unrealizedPL': '0.0000', 'units': '0', 'pl': '0.0000', 'resettablePL': '0.0000'}, 'pl': '-0.0086'}]}

List all open Positions for an Account.


In [7]:
r = positions.OpenPositions(accountID=accountID)

In [8]:
client.request(r)


Out[8]:
{'lastTransactionID': '77',
 'positions': [{'instrument': 'AUD_USD',
   'long': {'averagePrice': '0.75481',
    'pl': '0.0000',
    'resettablePL': '0.0000',
    'tradeIDs': ['31', '33'],
    'units': '200',
    'unrealizedPL': '0.2706'},
   'pl': '0.0000',
   'resettablePL': '0.0000',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.2706'},
  {'instrument': 'GBP_USD',
   'long': {'averagePrice': '1.25892',
    'pl': '0.2866',
    'resettablePL': '0.2866',
    'tradeIDs': ['74'],
    'units': '100',
    'unrealizedPL': '0.0555'},
   'pl': '0.2866',
   'resettablePL': '0.2866',
   'short': {'pl': '0.0000',
    'resettablePL': '0.0000',
    'units': '0',
    'unrealizedPL': '0.0000'},
   'unrealizedPL': '0.0555'}]}

Get the details of a single instrument’s position in an Account


In [9]:
instrument = "AUD_USD"

In [10]:
r = positions.PositionDetails(accountID=accountID, instrument=instrument)

In [11]:
client.request(r)


Out[11]:
{'lastTransactionID': '77',
 'position': {'instrument': 'AUD_USD',
  'long': {'averagePrice': '0.75481',
   'pl': '0.0000',
   'resettablePL': '0.0000',
   'tradeIDs': ['31', '33'],
   'units': '200',
   'unrealizedPL': '0.2706'},
  'pl': '0.0000',
  'resettablePL': '0.0000',
  'short': {'pl': '0.0000',
   'resettablePL': '0.0000',
   'units': '0',
   'unrealizedPL': '0.0000'},
  'unrealizedPL': '0.2706'}}

Closeout the open Position regarding instrument in an Account.


In [12]:
data = {
  "longUnits": "ALL"
}

In [13]:
r = positions.PositionClose(accountID=accountID,
                            instrument=instrument,
                            data=data)

In [14]:
client.request(r)


Out[14]:
{'lastTransactionID': '79',
 'longOrderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '78',
  'id': '78',
  'instrument': 'AUD_USD',
  'longPositionCloseout': {'instrument': 'AUD_USD', 'units': 'ALL'},
  'positionFill': 'REDUCE_ONLY',
  'reason': 'POSITION_CLOSEOUT',
  'time': '2017-01-30T01:58:38.167265656Z',
  'timeInForce': 'FOK',
  'type': 'MARKET_ORDER',
  'units': '-200',
  'userID': 5120068},
 'longOrderFillTransaction': {'accountBalance': '100002.2293',
  'accountID': '101-003-5120068-001',
  'batchID': '78',
  'financing': '0.0004',
  'id': '79',
  'instrument': 'AUD_USD',
  'orderID': '78',
  'pl': '0.2706',
  'price': '0.75576',
  'reason': 'MARKET_ORDER_POSITION_CLOSEOUT',
  'time': '2017-01-30T01:58:38.167265656Z',
  'tradesClosed': [{'financing': '0.0002',
    'realizedPL': '0.1239',
    'tradeID': '31',
    'units': '-100'},
   {'financing': '0.0002',
    'realizedPL': '0.1467',
    'tradeID': '33',
    'units': '-100'}],
  'type': 'ORDER_FILL',
  'units': '-200',
  'userID': 5120068},
 'relatedTransactionIDs': ['78', '79']}